package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.stylefeng.guns.base.auth.context.LoginContextHolder;
import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.sys.core.util.RedisUtils;
import cn.stylefeng.guns.sys.modular.system.entity.BtyBxda;
import cn.stylefeng.guns.sys.modular.system.entity.BtyZxBaoxiaodanAudit;
import cn.stylefeng.guns.sys.modular.system.entity.ZxChengbenMingxi;
import cn.stylefeng.guns.sys.modular.system.entity.ZxHuopin;
import cn.stylefeng.guns.sys.modular.system.mapper.BtyZxBaoxiaodanAuditMapper;
import cn.stylefeng.guns.sys.modular.system.mapper.ZxChengbenMingxiMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.BtyZxBaoxiaodanAuditParam;
import cn.stylefeng.guns.sys.modular.system.model.result.BtyZxBaoxiaodanAuditResult;
import cn.stylefeng.guns.sys.modular.system.service.BtyBxdaService;
import  cn.stylefeng.guns.sys.modular.system.service.BtyZxBaoxiaodanAuditService;
import cn.stylefeng.guns.sys.modular.system.service.ZxChengbenMingxiService;
import cn.stylefeng.guns.sys.modular.system.service.ZxHuopinService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxd
 * @since 2022-04-24
 */
@Service
public class BtyZxBaoxiaodanAuditServiceImpl extends ServiceImpl<BtyZxBaoxiaodanAuditMapper, BtyZxBaoxiaodanAudit> implements BtyZxBaoxiaodanAuditService {

    @Resource
    private BtyBxdaService btyBxdaService;

    @Resource
    private ZxHuopinService zxHuopinService;

    @Resource
    private ZxChengbenMingxiService zxChengbenMingxiService;

    @Resource
    private ZxChengbenMingxiMapper zxChengbenMingxiMapper;

    @Override
    public void add(BtyZxBaoxiaodanAuditParam param){
        BtyZxBaoxiaodanAudit entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(BtyZxBaoxiaodanAuditParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(BtyZxBaoxiaodanAuditParam param){
        BtyZxBaoxiaodanAudit oldEntity = getOldEntity(param);
        BtyZxBaoxiaodanAudit newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public BtyZxBaoxiaodanAuditResult findBySpec(BtyZxBaoxiaodanAuditParam param){
        return null;
    }

    @Override
    public List<BtyZxBaoxiaodanAuditResult> findListBySpec(BtyZxBaoxiaodanAuditParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(BtyZxBaoxiaodanAuditParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(BtyZxBaoxiaodanAuditParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private BtyZxBaoxiaodanAudit getOldEntity(BtyZxBaoxiaodanAuditParam param) {
        return this.getById(getKey(param));
    }

    private BtyZxBaoxiaodanAudit getEntity(BtyZxBaoxiaodanAuditParam param) {
        BtyZxBaoxiaodanAudit entity = new BtyZxBaoxiaodanAudit();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Resource
    private RedisUtils redisUtils;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void cbmxSave(BtyZxBaoxiaodanAuditParam param) {
        BtyZxBaoxiaodanAudit btyZxBaoxiaodanAudit = this.getById(param.getId());
        // 报销档案
        BtyBxda btyBxda = btyBxdaService.getById(btyZxBaoxiaodanAudit.getBxdaId());

        // 货品明细
        ZxHuopin zxHuopin = zxHuopinService.getById(btyZxBaoxiaodanAudit.getHuopinId());

        // 报销表
        param.setAuditTime(new Date());
        this.update(param);

        // 保存成本明细
        ZxChengbenMingxi zxChengbenMingxi = new ZxChengbenMingxi();
        zxChengbenMingxi.setJine(param.getRemitAmount());
        // 待审核
        zxChengbenMingxi.setStatus("3");
        zxChengbenMingxi.setCreateUser(btyZxBaoxiaodanAudit.getUserId());
        zxChengbenMingxi.setCreateUserName(btyZxBaoxiaodanAudit.getUserName());

        zxChengbenMingxi.setBtyyBxdaId(btyBxda.getYhk());
        zxChengbenMingxi.setBtyyBxdaIdView(btyBxda.getXm());
        zxChengbenMingxi.setHuopinId(btyZxBaoxiaodanAudit.getHuopinId());
        zxChengbenMingxi.setDm(btyZxBaoxiaodanAudit.getShopName());
        zxChengbenMingxi.setBxr(btyBxda.getXm() + "-" + btyBxda.getYhk());
        zxChengbenMingxi.setVoucherDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        // 门店id
        zxChengbenMingxi.setBtyZxMdzxId(btyZxBaoxiaodanAudit.getShopId());
        // 报销表id
        zxChengbenMingxi.setSrcValue(btyZxBaoxiaodanAudit.getId().toString());
        // 报销金额
        zxChengbenMingxi.setBxjine(btyZxBaoxiaodanAudit.getReimbursementAmount());
        // 科目
        zxChengbenMingxi.setBtyZxKemuId(zxHuopin.getBtyZxKemuId());
        // 其他科目
        zxChengbenMingxi.setQtkm(btyZxBaoxiaodanAudit.getProductRemark());
        // 软装
        zxChengbenMingxi.setMxType(2);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String code = sdf.format(new Date());
        Long xlh = redisUtils.incr(code);
        zxChengbenMingxi.setCompany("ZX" + code + String.format("%03d", xlh));
        // src_type
        zxChengbenMingxi.setSrcType("ZX" + code + String.format("%03d", xlh));
        zxChengbenMingxi.setCreateTime(new Date());
        // 存在更新不存在插入
        QueryWrapper qw = new QueryWrapper();
        qw.eq("src_value", btyZxBaoxiaodanAudit.getId().toString());
        List<ZxChengbenMingxi> list = zxChengbenMingxiMapper.selectList(qw);
        if (list.size() > 0) {
            zxChengbenMingxiService.update(zxChengbenMingxi, qw);
        } else {
            zxChengbenMingxiService.save(zxChengbenMingxi);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void financeReject(BtyZxBaoxiaodanAuditParam param) {

        String name = LoginContextHolder.getContext().getUser().getName();
        Long id = LoginContextHolder.getContext().getUser().getId();

        param.setReason(name + ": " + param.getReason());
        param.setAuditTime(new Date());
        this.update(param);

        // 明细表被打回 = 5
        ZxChengbenMingxi zxChengbenMingxi = new ZxChengbenMingxi();
        zxChengbenMingxi.setBtyZxChengbenMingxiId(param.getBtyZxChengbenMingxiId());
        zxChengbenMingxi.setStatus("5");
        zxChengbenMingxi.setUpdateUserName(name);
        zxChengbenMingxi.setUpdateUser(id);
        zxChengbenMingxi.setUpdateTime(new Date());
        zxChengbenMingxi.setCreateTime(new Date());
        zxChengbenMingxiService.updateById(zxChengbenMingxi);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void financeAgree(BtyZxBaoxiaodanAuditParam param) {
        String name = LoginContextHolder.getContext().getUser().getName();
        Long id = LoginContextHolder.getContext().getUser().getId();
        // 清除原因和驳回时间
        param.setReason("");
        param.setAuditTime(new Date());
        // 报销表
        this.update(param);

        // 明细表待打款 = 2 = null
        LambdaUpdateWrapper<ZxChengbenMingxi> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(ZxChengbenMingxi::getStatus, null);
        wrapper.eq(ZxChengbenMingxi::getBtyZxChengbenMingxiId, param.getBtyZxChengbenMingxiId());
        wrapper.set(ZxChengbenMingxi::getUpdateUserName, name);
        wrapper.set(ZxChengbenMingxi::getUpdateUser, id);
        wrapper.set(ZxChengbenMingxi::getCreateTime, new Date());
        wrapper.set(ZxChengbenMingxi::getUpdateTime, new Date());
        wrapper.set(ZxChengbenMingxi::getJine, param.getRemitAmount());
        wrapper.set(ZxChengbenMingxi::getBtyyBxdaId, param.getPayeeCard());
        wrapper.set(ZxChengbenMingxi::getBtyyBxdaIdView, param.getPayee());
        zxChengbenMingxiService.update(wrapper);


//        ZxChengbenMingxi zxChengbenMingxi = new ZxChengbenMingxi();
//        zxChengbenMingxi.setBtyZxChengbenMingxiId(param.getBtyZxChengbenMingxiId());
//        zxChengbenMingxi.setStatus(null);
//        zxChengbenMingxi.setUpdateUserName(name);
//        zxChengbenMingxi.setUpdateUser(id);
//        zxChengbenMingxi.setUpdateTime(new Date());
//        zxChengbenMingxi.setCreateTime(new Date());
//        zxChengbenMingxiService.updateById(zxChengbenMingxi);
    }
}
